<?php
// api.php

// 定义密钥（建议使用环境变量或更安全的方法来存储密钥）
define('SECRET_KEY', '68122ez3HEE7='); // 请替换为你的密钥

// 获取请求密钥并验证
$requestKey = isset($_GET['key']) ? $_GET['key'] : '';
if (!hash_equals(base64_encode(SECRET_KEY), $requestKey)) {
    http_response_code(403);
    echo json_encode(['success' => false, 'message' => '密钥无效']);
    exit;
}

// 用户目录
$userDir = 'user';

// 获取所有用户
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $users = array_filter(glob("$userDir/*"), 'is_dir');
    $userNames = array_map('basename', $users);
    echo json_encode(['success' => true, 'users' => $userNames]);
}

// 删除用户
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
    $userName = isset($_GET['username']) ? $_GET['username'] : '';
    $userPath = "$userDir/$userName";
    
    if (is_dir($userPath)) {
        array_map('unlink', glob("$userPath/*.*")); // 删除文件
        rmdir($userPath); // 删除文件夹
        echo json_encode(['success' => true, 'message' => "用户 $userName 删除成功"]);
    } else {
        echo json_encode(['success' => false, 'message' => "用户 $userName 不存在"]);
    }
}
?>